package com.kexl.activiti.demo.controller;

import com.kexl.activiti.demo.utils.RestMessage;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.util.CollectionUtil;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @classname: TaskController
 * @description:
 * @author: kexl
 * @create: 2021-03-16 13:43
 * @version: 1.0
 **/
@Slf4j
@RestController
public class TaskController {

    @Autowired
    private TaskService taskService;


    /**
     * 根据流程assignee查询当前人的个人任务
     * @param assignee
     * @return
     */
    @PostMapping(path = "findTaskByAssignee")
    public RestMessage findTaskByAssignee(@RequestParam("assignee") String assignee) {
        RestMessage restMessage = new RestMessage();

        try {
            //指定个人任务查询
            List<Task> taskList = taskService.createTaskQuery().taskAssignee(assignee).list();
            if (CollectionUtil.isNotEmpty(taskList)) {
                List<Map<String, String>> resultList = new ArrayList<>();
                for (Task task : taskList) {
                    Map<String, String> resultMap = new HashMap<>();
                    /* 任务ID */
                    resultMap.put("taskID", task.getId());
                    /* 任务名称 */
                    resultMap.put("taskName", task.getName());
                    /* 任务的创建时间 */
                    resultMap.put("taskCreateTime", task.getCreateTime().toString());
                    /* 任务的办理人 */
                    resultMap.put("taskAssignee", task.getAssignee());
                    /* 流程实例ID */
                    resultMap.put("processInstanceId", task.getProcessInstanceId());
                    /* 执行对象ID */
                    resultMap.put("executionId", task.getExecutionId());
                    /* 流程定义ID */
                    resultMap.put("processDefinitionId", task.getProcessDefinitionId());
                    resultList.add(resultMap);
                }
                restMessage = RestMessage.success("查询成功", resultList);
            } else {
                restMessage = RestMessage.success("查询成功", null);
            }
        } catch (Exception e) {
            restMessage = RestMessage.fail("查询失败", e.getMessage());
            log.error("根据流程assignee查询当前人的个人任务,异常:{}", e);
            return restMessage;
        }
        return restMessage;
    }

    /**
     * 完成任务，任务进入下一个节点
     * @param taskId
     * @param variables
     * @return
     */
    @PostMapping(path = "completeTask")
    public RestMessage completeTask(@RequestParam("taskId") String taskId, Map<String, Object> variables) {

        RestMessage restMessage = new RestMessage();
        try {
            taskService.complete(taskId, variables);
            restMessage = RestMessage.fail("完成任务成功", taskId);
        } catch (Exception e) {
            restMessage = RestMessage.fail("完成任务失败", e.getMessage());
            log.error("完成任务,异常:{}", e);
        }
        return restMessage;
    }


    /**
     * 任务转办，把任务交给别人处理
     * @param taskId
     * @param userKey
     * @return
     */
    @PostMapping(path = "turnTask")
    public RestMessage turnTask(@RequestParam("taskId") String taskId, @RequestParam("userKey") String userKey) {
        RestMessage restMessage = new RestMessage();
        try {
            taskService.setAssignee(taskId, userKey);
            restMessage = RestMessage.fail("完成任务成功", taskId);
        } catch (Exception e) {
            restMessage = RestMessage.fail("完成任务失败", e.getMessage());
            log.error("任务转办,异常:{}", e);
        }
        return restMessage;
    }
}

